import axios from 'axios'
import uuidv1 from "uuid/v1";
let mixin = {
  props: {

  },
  mounted() {
  },
  data() {
    return {
      gongreds: [],
      gongrecolor: {
        "zhengzhou-model-0-yes.json": '#b399ffb3',
        "zhengzhou-model-0-no.json": '#c0c0c0b3',
        "zhengzhou-model-1-yes.json": '#00ffffb3',
        "zhengzhou-model-1-no.json": '#c0c0c0b3',
      },

      pointericon: {
        '隔压站': 'geyazhan-01.png',
        '燃气锅炉房': 'guolufang-01.png',
        '热电厂': 'redianchang-01.png',
      },

      showdetailpointer: false,
      showteshupointer: false,

      showheight: 9000,

    }
  },
  watch: {
    camheight: {
      handler: function (n, o) {
        this.zoomgongre(n)
      }
    }
  },
  methods: {
    //摄像机变化
    zoomgongre(n) {
      let _this = this
      //判断是否初始化
      if (this.ds.length < 3) {
        return
      }
      //判断是否为供热
      if (this.$store.state.daping.scencetype != 'gongre') {
        return
      }

      //全局设置是否显示
      //如果小于1000并且不显示
      if (n < this.showheight && !this.showdetailpointer) {
        var entities = {}
        for (
          let index = 0;
          index < this._viewer.dataSources.length;
          index++
        ) {
          entities = this._viewer.dataSources.get(index).entities.values
          for (let i = 0; i < entities.length; i++) {
            let entity = entities[i]
            if (entity.id.split(".")[0] == 'tee' || entity.id.split(".")[0] == 'substation') {
              //判断视角高度
              //判断是否已经显示
              //普通点
              //------------------------------------++++++++++++++++++++++++++++++
              if (entity.properties.name._value != "") {
                if (entity.properties.name._value.indexOf('T') == -1 && entity.properties.name._value.indexOf('V') == -1) {
                  entity.point = new Cesium.PointGraphics({
                    color: Cesium.Color.fromCssColorString("#5D96B7"),
                    pixelSize: 20
                  });
                }
              }
            }
          }
        }
        this.showdetailpointer = true
      }
      //如果大于1000并且显示
      if (n > this.showheight && this.showdetailpointer) {

        this.showdetailpointer = false
        var entities = {}
        for (
          let index = 0;
          index < this._viewer.dataSources.length;
          index++
        ) {
          entities = this._viewer.dataSources.get(index).entities.values
          for (let i = 0; i < entities.length; i++) {
            let entity = entities[i]
            if (entity.id.split(".")[0] == 'tee' || entity.id.split(".")[0] == 'substation') {
              //判断视角高度
              //判断是否已经显示
              //普通点
              //------------------------------------++++++++++++++++++++++++++++++
              if (entity.properties.name._value != "") {
                if (entity.properties.name._value.indexOf('T') == -1 && entity.properties.name._value.indexOf('V') == -1) {
                  entity.point = null
                }
              }
            }
          }
        }
      }

    },
    //初始化方法
    gongre() {
      let _this = this;
      var viewer = this._earth.czm.viewer;
      let geos = [
        "zhengzhou-model-0-yes.json",
        "zhengzhou-model-0-no.json",
        "zhengzhou-model-1-yes.json",
        "zhengzhou-model-1-no.json"
      ];

      geos.map(item => {
        var promise = Cesium.GeoJsonDataSource.load("/geojson/" + item);
        let tmp_color = item
        promise.then(function (dataSource) {
          viewer.dataSources.add(dataSource);
          // _this.ds.push(dataSource._name);
          var entities = dataSource.entities.values;

          for (var o = 0; o < entities.length; o++) {
            var entity = entities[o];

            //删除默认提示框
            entity.billboard = undefined;

            //判断点
            if (entity.id.split(".")[0] == 'heatplant') {
              //特殊点
              //热源厂 需要区分隔压站 锅炉房 热源
              switch (entity.properties._type._value) {
                case '隔压站':
                case '燃气锅炉房':
                case '热电厂':
                  if (entity.properties.name._value != "") {
                    if (entity.properties.name._value.indexOf('T') == -1 && entity.properties.name._value.indexOf('V') == -1) {

                      // entity.point = new Cesium.PointGraphics({
                      //   color: Cesium.Color.fromCssColorString("#DB181C"),
                      //   pixelSize: 40
                      // });

                      let nameid = entity.id + '|' + entity.properties.pid._value + '|' + entity.position._value

                      let ln = Cesium.Cartographic.fromCartesian(
                        entity.position._value
                      )

                      let uniqueID = uuidv1();
                      _this._earth.sceneTree.root.children.push(
                        {
                          "ref": uniqueID,
                          "czmObject": {
                            "customProp": 'ghjghj',
                            "xbsjType": "Pin",
                            "xbsjGuid": "96f2033d-55cf-4677-b8c7-bde52f14b054",
                            "name": nameid,
                            "position": [
                              ln.longitude,
                              ln.latitude,
                              ln.height
                            ],
                            "scale": 0.6,
                            // disableDepthTestDistance: 0,
                            "imageUrl": "/configuration/logo/" + _this.pointericon[entity.properties._type._value],
                            "pinBuilder": {},
                            "far": 1073741824
                          }
                        }
                      )

                      _this.pinarr.push(uniqueID)


                      // let pinn = _this._earth.sceneTree.$refs[entity.id].czmObject;
                      // pinn.onclick = () => {

                      // };

                      // pinn.onmouseover = () => {
                      //   pinn.scale = 1.1;
                      // }
                      // pinn.onmouseout = () => {
                      //   pinn.scale = 1.0;
                      // }



                    }
                  }
                  break;
                default:
                  break;
              }

            } else if (entity.id.split(".")[0] == 'substation' || entity.id.split(".")[0] == 'tee') {
              //普通点
              //热力站
              if (entity.properties.name._value != "") {
                if (entity.properties.name._value.indexOf('T') == -1 && entity.properties.name._value.indexOf('V') == -1) {
                  // entity.point = new Cesium.PointGraphics({
                  //   color: Cesium.Color.fromCssColorString("#55DD2F55"),
                  //   pixelSize: 20
                  // });
                }
              }

            } else if (entity.id.split(".")[0] == 'pipeline') {

              let positions = []
              if (entity.polyline.positions && entity.polyline.positions._value && entity.polyline.positions._value.length) {
                for (let index = 0; index < entity.polyline.positions._value.length; index++) {
                  let aaa = Cesium.Cartographic.fromCartesian(entity.polyline.positions._value[index])
                  aaa.height = _.random(50, 50)
                  var ellipsoid = _this._viewer.scene.globe.ellipsoid;
                  var cartesian3 = ellipsoid.cartographicToCartesian(aaa);
                  positions.push(cartesian3)
                }
                entity.polyline.positions._value = positions
              }


              // positions

              entity.polyline.width = 20;  //添加默认样式
              entity.polyline.material = new Cesium.PolylineGlowMaterialProperty({
                glowPower: .2, //一个数字属性，指定发光强度，占总线宽的百分比。
                // color: Cesium.Color.fromCssColorString(_this.gongrecolor[tmp_color])
                color: Cesium.Color.fromCssColorString("#F4F3EC99")

              })

            }
          }
        });
      });

      //添加点击事件
      viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
        movement
      ) {
        var pickedFeature = viewer.scene.pick(movement.position);
        if (typeof pickedFeature != "undefined") {
          if (pickedFeature.id.properties == undefined) {
            //添加点击事件
            _this.pointerclick({
              id: pickedFeature.id.name.split("|")[0],
              pid: pickedFeature.id.name.split("|")[1],
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });

          } else {
            //鼠标是否点到线上
            _this.pointerclick({
              id: pickedFeature.id.id,
              pid: pickedFeature.id.properties.pid._value,
              position: Cesium.Cartographic.fromCartesian(
                pickedFeature.id.position._value
              )
            });
          }

        }
      },
        Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },

    pointerclick(options) {
      //获取接口数据
      //数据返回显示弹窗
      //判断id
      let type = options.id.split(".")[0] == 'substation' ? 'substation' : 'heatplant'
      let pid = options.pid


      let requestBody = { // 热源
        "queries": [{
          "metric": "pid,name,m_001t,m_001p,m_002p,m_001q",
          "schema": "monitor"
        }],
        "table": "HeatPlant",
        "search": "pid=" + pid
      };
      if (type == "substation") {
        requestBody = { // 热力站
          "queries": [{
            "metric": "pid,name,m_001t,m_001p,m_002p,m_003q,m_003opening",
            "schema": "monitor"
          }],
          "table": "SubStation",
          "search": "pid=" + pid
        };
      }

      if (this.ishistory == "历史") {
        requestBody = {
          "queries": [{
            "metric": "pid,name,m_001t,m_001p,m_002p,m_001q",
            "schema": "monitor"
          }],
          "table": "HeatPlant",
          "search": "pid=" + pid,
          "type": "history",
          startTime: this.timerangarr[0].getTime(),
          endTime: this.timerangarr[1].getTime()
        };
        if (type == "substation") {
          requestBody = { // 热力站
            "queries": [{
              "metric": "pid,name,m_001t,m_001p,m_002p,m_003q,m_003opening",
              "schema": "monitor"
            }],
            "table": "SubStation",
            "search": "pid=" + pid,
            "type": "history",
            startTime: this.timerangarr[0].getTime(),
            endTime: this.timerangarr[1].getTime()
          };
        }
      }

      axios.post("/engipower/open/queryDb.do", requestBody).then(res => {
        //如果有则获取200中的结果
        let data = res.data
        let html = ''
        if (data.allTotal != 0) {
          if (type == "heatplant") {
            html = '<div style="font-size:10px;">' +
              '<p>名称: ' + data["200"][0].name + '</p>\n' +
              '<p>供压: ' + ((data["200"][0].m_001p) ? (data["200"][0].m_001p) : '-') + ' MPa</p>\n' +
              '<p>回压: ' + ((data["200"][0].m_002p) ? (data["200"][0].m_002p) : '-') + ' MPa</p>\n' +
              '<p>流量: ' + ((data["200"][0].m_001q) ? (data["200"][0].m_001q) : '-') + ' t/h</p>\n' +
              '<p>供温: ' + ((data["200"][0].m_001t) ? (data["200"][0].m_001t) : '-') + ' ℃</p>' + '</div>';
          }

          if (type == "substation") {
            html = '<div style="font-size:10px;">' +
              '<p>名称: ' + data["200"][0].name + '</p>\n' +
              '<p>一次供压: ' + ((data["200"][0].m_001p) ? (data["200"][0].m_001p) : '-') + ' MPa</p>\n' +
              '<p>一次回压: ' + ((data["200"][0].m_002p) ? (data["200"][0].m_002p) : '-') + ' MPa</p>\n' +
              '<p>瞬时流量: ' + ((data["200"][0].m_003q) ? (data["200"][0].m_003q) : '-') + ' t/h</p>\n' +
              '<p>一次供温: ' + ((data["200"][0].m_001t) ? (data["200"][0].m_001t) : '-') + ' ℃</p>\n' +
              '<p>阀门开度: ' + ((data["200"][0].m_003opening) ? (data["200"][0].m_003opening) : '-') + ' %</p>' + '</div>';
          }
        } else {
          html = '<div style="font-size:10px;">' +
            // '<p>名称: ' + feature.properties.name + '</p>\n' +
            '<p>未获取到该点数据' + '</p>\n' + '</div>';
        }

        let uniqueID = uuidv1()
        options.uid = uniqueID
        this.msgboxarr.push({ uid: uniqueID, diahtml: html, options: options })
      });
    },




  }
}

export default mixin